package com.juben.svr.demos.web;

import com.juben.svr.domain.ApiResponse;
import com.juben.svr.domain.Conversations;
import com.juben.svr.domain.jb_messages;
import com.juben.svr.mapper.ConversationMapper;
import com.juben.svr.mapper.JubenMessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin(origins = "http://localhost:5173",allowedHeaders = "*",allowCredentials = "true")
public class JuBenMessageController {
    @Autowired
    private JubenMessageMapper jubenMessageMapper;
    @Autowired
    private ConversationMapper conversationMapper;
    //消息列表
    @GetMapping("/getMessageList")
    public ResponseEntity<ApiResponse> msgList(@RequestParam Integer sessionId){
        Conversations conversations=conversationMapper.getConversationByID(sessionId);
        if (conversations == null){
            return ResponseEntity.ok(new ApiResponse(200,"还没有开始会话",null));
        }
        Integer fromId=conversations.getUser_id();
        Integer toId=conversations.getToUserId();
        System.out.println("fromId:"+fromId+"toId:"+toId);
        List<jb_messages> msgInfoList =  jubenMessageMapper.selectMsgList(fromId,toId);
        System.out.println("msgInfoList:::::::" + msgInfoList);
        //更新消息为已读
        jubenMessageMapper.msgRead(fromId,toId);
        //更新会话里的未读消息
        conversationMapper.delUnReadCount(fromId,toId);
        return ResponseEntity.ok(new ApiResponse(200,"获取消息列表成功",msgInfoList));
    }
}
